home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Code Resources / Arrow CDEF / ArrowCDEF.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-29  |  6.6 KB  |  211 lines  |  [TEXT/KAHL]

  1. /******************************************************************
  2.     FILE:            ArrowCDEF.c
  3.     CREATED:        March 16, 1994
  4.     AUTHOR:        David Hay
  5.     
  6.     Copyright (C) 1994  David Hay
  7.     
  8.     This program is free software; you can redistribute it and/or modify
  9.     it under the terms of the GNU General Public License as published by
  10.     the Free Software Foundation; either version 2 of the License, or
  11.     (at your option) any later version.
  12.     
  13.     This program is distributed in the hope that it will be useful,
  14.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.     GNU General Public License for more details.
  17.     
  18.     You should have received a copy of the GNU General Public License
  19.     along with this program; if not, write to the Free Software
  20.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.     
  22.     Comments and questions are welcome:
  23.     E-mail:    hay@cs.colorado.edu
  24.     US Mail:    David Hay
  25.             117 Piedra Loop
  26.             Los Alamos, NM 87544
  27.     
  28.     Thanks to Eddy J. Gurney for helping a novice to Macintosh programming (me) and for
  29.     showing me that Geneva 9 pt is a really cool font to program in!
  30. ******************************************************************/
  31.  
  32. #include <SetUpA4.h>
  33.  
  34. #include "ArrowCDEF.h"
  35.  
  36. #define    kInactive        255        /*    part code indicating the control is inactive        */
  37.  
  38. const unsigned char copyright[] = "Arrow CDEF 1.0 ©1994 David Hay.";
  39.  
  40. struct ControlResData            /*    The data we get from the resource    */
  41. {
  42.     short    plainPictID;        /*    The PICT to draw for an unhilited arrow            */
  43.     short    upPictID;            /*    The PICT to draw when the up arrow is pressed        */
  44.     short    downPictID;        /*    The PICT to draw then the down arrow is pressed    */
  45.     short    inactivePictID;        /*    The PICT to draw when the arrow is inactive        */
  46. };
  47.  
  48. typedef struct ControlResData ControlResData;
  49. typedef ControlResData **CntlResDataHndl;
  50.  
  51.  
  52. struct ControlData            /*    the data we store in the contrlData field of the control    */
  53. {
  54.     ControlResData        picts;    /*    The PICTs to draw                             */
  55.     short            resFile;    /*    The resource file to use, since it might change on us    */
  56. };
  57.  
  58. typedef struct ControlData ControlData;
  59. typedef ControlData **ControlDataHandle;
  60.  
  61.  
  62.  
  63. pascal long main( short variation, ControlHandle theControl, short msg, long param )
  64. {
  65.     long                    result;        /*    the result to return        */
  66.     ControlDataHandle        control;        /*    the control data            */
  67.  
  68.     RememberA0();
  69.     SetUpA4();
  70.  
  71.     result = 0;
  72.  
  73.     switch ( msg )
  74.     {
  75.         case initCntl:        /*    initialize the arrow control data        */
  76.         {
  77.             short                arrowResID;    /*    res ID of the resource holding the pict info    */
  78.             CntlResDataHndl        picts;        /*    res handle to hold which picts to use        */
  79.             
  80.             arrowResID = GetCRefCon( theControl );
  81.             picts = (CntlResDataHndl) Get1Resource( kArrowResType, arrowResID );
  82.             control = (ControlDataHandle)NewHandleClear( sizeof( ControlData ) );
  83.             BlockMove( *picts, *control, sizeof( ControlResData ) );
  84.             (*control)->resFile = CurResFile();        /*    save the res file in case it changes    */
  85.             (*theControl)->contrlData = (Handle)control;
  86.             break;
  87.         }
  88.  
  89.         case drawCntl:        /*    Draw the arrow control                */
  90.         {
  91.             PicHandle            thePict;        /*    the PICT to draw            */
  92.             unsigned char        hilite;        /*    the current control hilite state    */
  93.             Rect                drawRect;        /*    the rect to draw the PICT in    */
  94.             short            pictID;        /*    the resource ID of the PICT    */
  95.             short            curRes;        /*    the current resource file        */
  96.             
  97.             if ( (*theControl)->contrlVis )
  98.             {
  99.                 thePict = NULL;
  100.                 hilite = (*theControl)->contrlHilite;
  101.                 control = (ControlDataHandle)(*theControl)->contrlData;
  102.                 drawRect = (*theControl)->contrlRect;
  103.                 switch( hilite )
  104.                 {
  105.                     case 0:            /*    no hiliting             */
  106.                         pictID = (*control)->picts.plainPictID;
  107.                         break;
  108.                     case inUpButton:    /*    up arrow hilited    */
  109.                         pictID = (*control)->picts.upPictID;
  110.                         break;
  111.                     case inDownButton:    /*    down arrow hilited    */
  112.                         pictID = (*control)->picts.downPictID;
  113.                         break;
  114.                     case kInactive:        /*    inactive control        */
  115.                         pictID = (*control)->picts.inactivePictID;
  116.                         break;
  117.                     default:            /* any other part codes don't apply */
  118.                         pictID = -1;
  119.                         break;
  120.                 }
  121.                 if ( pictID >= 0 )    /*    will probably always be true, but better safe than sorry!    */
  122.                 {
  123.                     curRes = CurResFile();            /*    save the current res file        */
  124.                     UseResFile( (*control)->resFile );    /*    change to the saved res file    */
  125.                     thePict = GetPicture( pictID );        /*    Get the picture, draw it, if...    */
  126.                     if ( thePict )                    /*    ...possible, then release it.    */
  127.                     {
  128.                         if ( hilite == kInactive )        /*    Erase the arrow only when...    */
  129.                             EraseRect( &drawRect );    /*    ...it needs to be deactivated    */
  130.                         DrawPicture( thePict, &drawRect );
  131.                         ReleaseResource( thePict );
  132.                     }
  133.                     UseResFile( curRes );            /*    resore the old res file        */
  134.                 }
  135.             }
  136.             break;
  137.         }
  138.  
  139.         case testCntl:        /*    Determine which part of the arrow the mouse is in    */
  140.         {
  141.             Point            mousePoint;    /*    where the mouse is                */
  142.             Rect            upRect;        /*    rect comprimising the up arrow    */
  143.             Rect            downRect;        /*    rect comprimising the down arrow    */
  144.             Rect            plainRect;        /*    the entire arrow rect            */
  145.                         
  146.             plainRect = (*theControl)->contrlRect;
  147.             mousePoint.v = HiWord( param );
  148.             mousePoint.h = LoWord( param );
  149.  
  150.             /*    If the mouse point is in the control and it is active,
  151.                 determine which part the mouse went down in.            */
  152.             if ( (*theControl)->contrlHilite != kInactive && PtInRect( mousePoint, &plainRect ) )
  153.             {
  154.                 /*    Assume the up arrow and down arrow each take half the control    */
  155.                 upRect = plainRect;
  156.                 upRect.bottom -= (plainRect.bottom - plainRect.top) / 2;
  157.                 downRect = plainRect;
  158.                 downRect.top = upRect.bottom - 1;
  159.  
  160.                 if ( PtInRect( mousePoint, &upRect ) )            /*    up arrow pressed    */
  161.                 {
  162.                     result = inUpButton;
  163.                 }
  164.                 else if ( PtInRect( mousePoint, &downRect ) )        /*    down arrow pressed    */
  165.                 {
  166.                     result = inDownButton;
  167.                 }
  168.             }
  169.             break;
  170.         }
  171.         
  172.         case calcCRgns:    /*    This is the message sent if using 24-bit addressing    */
  173.             param = (long)StripAddress((Ptr)param);        /* Mask off the high byte if necessary */
  174.             /*    Fall through on purpose!    */
  175.  
  176.         case calcCntlRgn:    /*    Under 32-bit addressing we get this message        */
  177.             RectRgn((RgnHandle)param, &(*theControl)->contrlRect);
  178.             break;
  179.         
  180.         case dispCntl:        /*    Free the data stored in the control                */
  181.             if ( (*theControl)->contrlData );
  182.             {
  183.                 DisposeHandle( (*theControl)->contrlData );
  184.                 (*theControl)->contrlData = NULL;
  185.             }
  186.             break;
  187.  
  188.     #if 0    
  189.         case posCntl:
  190.         case thumbCntl:
  191.         case dragCntl:
  192.         case autoTrack:
  193.         case undoDev:
  194.         case cutDev:
  195.         case copyDev:
  196.         case pasteDev:
  197.         case clearDev:
  198.         case cursorDev:
  199.         case calcThumbRgn:
  200.             break;    /*    We don't respond to any of these messages    */
  201.     #endif
  202.  
  203.         default:    /*    Any other messages we don't handle or which aren't defined yet    */
  204.             break;
  205.     }
  206.  
  207.     RestoreA4();
  208.  
  209.     return( result );
  210. }
  211.